$(document).ready(function(){
	$('#grid').datagrid({
		url: baseUrl + '/manager/role/list/a/rows',
		sortName: 'id',
		sortOrder: 'desc',
		remoteSort: false,
		nowrap: false,
		striped: true,
		idField: 'role_id',
		frozenColumns: [[
		  {field: 'ck', checkbox: true},
		  {title:'编号',field:'id',width:40,sortable:true, align:'center'}
		]],
		pagination: true,
		rownumbers:true,
		toolbar: [{
			text: '新增角色',
			iconCls: 'add',
			handler: function() {
				window.location = baseUrl + '/manager/role/add';
			}
		},'-',{
			text: '修改角色',
			iconCls: 'edit',
			handler: editrole
		},'-',{
			text: '删除角色',
			iconCls: 'delete',
			handler: deleterole
		}]
	});
	
	$('#search-btn').click(function(){
		$('#grid').datagrid('load', {
			search: '123'
		});
		
	});
	
	
	//用于权限列表
	
	$('#checkbox-all').click(function(){
		if($(this).attr('checked')) {
			$('#acl-table input[type=checkbox]').each(function(i){
				$(this).attr('checked', true);
			});
		} else {
			$('#acl-table input[type=checkbox]').each(function(i){
				$(this).attr('checked', false);
			});
		}
	});
	
	$('#user-all').click(function(){
		if($(this).attr('checked')) {
			$('#acl_user input[type=checkbox]').each(function(i){
				$(this).attr('checked', true);
			});
		} else {
			$('#acl_user input[type=checkbox]').each(function(i){
				$(this).attr('checked', false);
			});
		}
	});
	
	$('#goods-all').click(function(){
		if($(this).attr('checked')) {
			$('#acl_goods input[type=checkbox]').each(function(i){
				$(this).attr('checked', true);
			});
		} else {
			$('#acl_goods input[type=checkbox]').each(function(i){
				$(this).attr('checked', false);
			});
		}
	});
	
	$('#article-all').click(function(){
		if($(this).attr('checked')) {
			$('#acl_article input[type=checkbox]').each(function(i){
				$(this).attr('checked', true);
			});
		} else {
			$('#acl_article input[type=checkbox]').each(function(i){
				$(this).attr('checked', false);
			});
		}
	});
	
	$('#order-all').click(function(){
		if($(this).attr('checked')) {
			$('#acl_order input[type=checkbox]').each(function(i){
				$(this).attr('checked', true);
			});
		} else {
			$('#acl_order input[type=checkbox]').each(function(i){
				$(this).attr('checked', false);
			});
		}
	});
	
	$('#template-all').click(function(){
		if($(this).attr('checked')) {
			$('#acl_template input[type=checkbox]').each(function(i){
				$(this).attr('checked', true);
			});
		} else {
			$('#acl_template input[type=checkbox]').each(function(i){
				$(this).attr('checked', false);
			});
		}
	});
	
	$('#other-all').click(function(){
		if($(this).attr('checked')) {
			$('#acl_other input[type=checkbox]').each(function(i){
				$(this).attr('checked', true);
			});
		} else {
			$('#acl_other input[type=checkbox]').each(function(i){
				$(this).attr('checked', false);
			});
		}
	});
	
	$('#role').submit(function(){
		//提交表单
		$.ajax({
			url: baseUrl + '/manager/role/add/a/submit',
			type: 'post',
			data: $('#role').serialize(),
			dataType: 'json',
			success: function(json) {
				if(!json.success) {
					$.messager.alert('警告', json.message, 'warning');
					return false;
				}
				
				window.location = baseUrl + '/manager/role/list';
			}
		});
	});
	
	$('#role_edit').submit(function(){
		//提交表单
		$.ajax({
			url: baseUrl + '/manager/role/edit/a/submit',
			type: 'post',
			data: $('#role_edit').serialize(),
			dataType: 'json',
			success: function(json) {
				if(!json.success) {
					$.messager.alert('警告', json.message, 'warning');
					return false;
				}
				
				window.location = baseUrl + '/manager/role/list';
			}
		});
	});
	
	roleAcl();
});

var roleAcl = function() {
	var val = new Array();
	$('#edit-roleacl input[type=checkbox]').each(function(i){
			val.push($(this).val());
	});
	
	$.ajax({
		url: baseUrl + '/manager/role/edit/a/permission',
		type: 'post',
		data: {id: id, role: role, data: eval(val)},
		dataType: 'json',
		success: function(json) {
			
			for(var i = 0; i < json.length; i ++) {
				
				$('#edit-roleacl input[type=checkbox]').eq(i).attr('checked', json[i]);
			}
		}
	});
	
};

var editrole = function() {
	var selected = $('#grid').datagrid('getSelected');
	if(selected) {
		window.location = baseUrl + '/manager/role/edit/id/' + selected.id;
		return true;
	}
	
	$.messager.alert('警告', '请先选择一条数据', 'warning');
};

var deleterole = function() {
	var ids = new Array();
	var rows = $('#grid').datagrid('getSelections');
	
	if(rows != null && rows != '') {
		for(var i = 0; i < rows.length; i ++) {
			ids.push(rows[i].role_id);
		}
		
		//ajax提交
		$.ajax({
			url: baseUrl + '/manager/role/delete',
			type: 'post',
			data: {ids: ids.join(',')},
			dataType: 'json',
			success: function(json) {
				
			}
		});
		
		return true;
	}
	
	$.messager.alert('警告', '至少请先选择一条数据', 'warning');
};